﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInformation;

namespace WebsiteAdmin.Controllers
{
    [Authorize]
    public class NewsTypeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllNewsTypeByPage(StoreRequestParameters parms)
        {
            WI_NewsTypeDAL tdal = new WI_NewsTypeDAL();
            int ttlCount;
            List<WI_NewsType> dbtypelist = tdal.GetAllNewsType(parms.Start, parms.Limit, out ttlCount);
            List<NewsType> typelist = new List<NewsType>();
            foreach (var dbtype in dbtypelist)
            {
                NewsType ntype = NewsType.AsNewsType(dbtype);
                typelist.Add(ntype);
            }
            return this.Store(typelist, ttlCount);
        }

        public ActionResult GetAllNewsType()
        {
            WI_NewsTypeDAL tdal = new WI_NewsTypeDAL();
            List<WI_NewsType> dbtypelist = tdal.GetAllNewsType();
            List<NewsType> typelist = new List<NewsType>();
            foreach (var dbtype in dbtypelist)
            {
                NewsType ntype = NewsType.AsNewsType(dbtype);
                typelist.Add(ntype);
            }
            return this.Store(typelist);
        }

        [HttpPost]
        public ActionResult DeleteNewsType(List<NewsType> delNewsType)
        {
            try
            {
                WI_NewsTypeDAL ntdal = new WI_NewsTypeDAL();
                foreach (var ntype in delNewsType)
                {
                    ntdal.RemoveNewsType(ntype.TypeID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewNewsTypeWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建类别",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateNewsType"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateNewsType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateNewsType(NewsType v_ntype)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                string creator = Request.IsAuthenticated ? HttpContext.User.Identity.Name : "Anonymous";
                v_ntype.Creator = creator;
                WI_NewsTypeDAL ntdal = new WI_NewsTypeDAL();
                ntdal.CreateNewsType(v_ntype);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "类别创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditNewsTypeWindow(int typeId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑类别",
                Width = 500,
                Height = 300,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditNewsType"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("typeId", typeId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditNewsType(int typeId)
        {
            WI_NewsTypeDAL ntdal = new WI_NewsTypeDAL();
            WI_NewsType wiNtype = ntdal.GetNewsType(typeId);
            NewsType ntype = NewsType.AsNewsType(wiNtype);
            return View(ntype);
        }

        [HttpPost]
        public ActionResult EditNewsType(NewsType ntype)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_NewsTypeDAL ntdal = new WI_NewsTypeDAL();
                ntdal.UpdateNewsType(ntype.TypeID, ntype);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "类别更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
